package com.hrbxlh.actionImpl;


import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.dao.*;
import com.hrbxlh.util.*;
import com.hrbxlh.vo.ClassTabVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.util.CollectionUtils;

/**
 * -------------------------------------------------------
 * Copyright (c) 2017, 鑫联华信息科技股份有限公司
 * All rights reserved.
 * FileName：RelationActionImpl.java
 * Description：课程表排课actionImpl类
 * History：
 * Date           Author               Desc
 * 2017.04.18	  xzx                  建立
 * -------------------------------------------------------
 */
@Component("relationActionImpl")
@Transactional
public class RelationActionImpl {

	@Resource(name = "relationDao")
	private RelationDao relationDao;

	@Resource(name = "myCourseDao")
	private MyCourseDao myCourseDao;

	@Resource(name = "classDao")
	private ClassDao classDao;

	@Resource(name = "courseDao")
	private CourseDao courseDao;

	@Resource(name = "paramActionImpl")
	private ParamActionImpl paramActionImpl;

	@Resource(name = "courseActionImpl")
	private CourseActionImpl courseActionImpl;

	@Resource(name = "relStuClassDao")
	private RelStuClassDao relStuClassDao;

	@Resource(name = "homeworkTestDao")
	private HomeworkTestDao homeworkTestDao;

	@Resource(name="questionDao")
	private QuestionDao questionDao;

	@Resource(name="sourceDao")
	private SourceDao sourceDao;

	@Autowired
	@Qualifier("classActionImpl")
	private ClassActionImpl classActionImpl;


	org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(RelationActionImpl.class.getName());



	/**
	 * @description 保存手动排课
	 * @param Map<String,Object> paramMap
	 * @return Map<String, String> result true成功，false失败failMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> saveClassTab(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//获取课表
			List<Map<String, Object>> paramList = (List<Map<String, Object>>)paramMap.get("tabList");
			//获取上课时间的参数
			Map<String, Object> paramTimeMap = paramActionImpl.matchClassNumAndTime();
			//查询是否可以进行排课（地点是否冲突，教师是否有课）
			Map<String,Object> addMarkMap = this.getSaveTabMark(paramList);
			boolean saveMark = (Boolean)addMarkMap.get("result");
			String className = "";
			//可以进行排课
			if(saveMark){
				Map<String, Object> classMap;
				//遍历参数
				for(int i = 0;i < paramList.size(); i++){
					//如果是选课的
					if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2){
						//获取申请班级的名称
						className = this.getRandomClassName(paramList.get(i));
					}
					//如果生成了选课班级名，并且这条是学生选课 或 是教师排课
					if(className != null && !className.equals("") && Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2 || Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 1){
						//保存课程的标识（教师排课还是学生选课）
						courseDao.saveSelectMark(paramList.get(i));
						//整理参数信息，存入课程表信息
						paramList.get(i).put("startTime", paramTimeMap.get(paramList.get(i).get("classNum").toString()+"start"));
						paramList.get(i).put("endTime", paramTimeMap.get(paramList.get(i).get("classNum").toString()+"end"));
						//如果是教师排课
						if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 1){
							paramList.get(i).put("addMark",1);
							//直接存入课程表
							relationDao.saveClassTab(paramList.get(i));
							//如果是选课
						}else{
							//查询原来是不是有这个课程对应的选课班级信息
							List<Map<String, Object>> selectClassesList = relationDao.getSelectClass(paramList.get(i));
							//如果原来有选课班级信息，直接存入参数
							if(selectClassesList != null && selectClassesList.size() != 0){
								paramList.get(i).put("classId", selectClassesList.get(0).get("classId"));
								//原来没有选课班级信息，新建选课班级并将信息存入参数中
							}else{
								classMap = new HashMap<String, Object>();
								classMap.put("className", className);
								//添加选课班级
								relationDao.addSelectClass(classMap);
								//将选课班级id存入参数
								paramList.get(i).put("classId", classMap.get("classId"));
								//查询当前教师当前课程是否已经有试题And将班级与试卷的关联插入关联表中
								Map<String,Object> relMap = new HashMap<>();
								relMap.put("classId",classMap.get("classId"));
								relMap.put("stuorteaId",paramList.get(i).get("stuorteaId"));
								relMap.put("courseId",paramList.get(i).get("courseId"));
								classActionImpl.addReOnlineClass(relMap);
							}
							//0504修改，保存选课的课表信息
							relationDao.saveClassTab(paramList.get(i));
						}
						//3、保存课程与班级关联信息
						List<Map<String, Object>> relCouClassList = relationDao.getRelCourseClass(paramList.get(i));
						//学生选课，保存 课程与班级关联信息（注意是不是已经有了关联信息）
						if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2){
							if(relCouClassList == null || relCouClassList.size() == 0){
								relationDao.saveRelCourseClass(paramList.get(i));
							}
						}
						//4、保存老师与课程的关系（注意给标识1或2） （注意是不是已经有了）
						List<Map<String, Object>> relCouTeaList = relationDao.getRelCourseTea(paramList.get(i));
						if(relCouTeaList == null || relCouTeaList.size() == 0){
							relationDao.saveRelCourseTea(paramList.get(i));
						}
						//0509添加的，有教师与课程的关系，将inMark修改为0
						else{
							paramList.get(i).put("inMark", 0);
							relationDao.saveInMarkReMark(paramList.get(i));
						}
						//学生选课，
						if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2){
							//6、教师与班级进行关联 （注意是不是已经有了）
							List<Map<String, Object>> relClassTeaList = relationDao.getRelClassTea(paramList.get(i));
							if(relClassTeaList == null || relClassTeaList.size() == 0){
								relationDao.saveRelClassTea(paramList.get(i));
							}
						}
						//修改课程的标识
						this.saveCourseSelect(paramList.get(i));
						//查询某个老师教的都是什么类型的课，如果有1的也有2的，就把这个人跟课的关联信息改为3
						//paramList.get(i).put("myMark", 1);
						//查询这个教师，这个课的正常关联信息
						List<Map<String, Object>> oneList = relationDao.getNormalClassRelation(paramList.get(i));
						paramList.get(i).put("myMark", 2);
						List<Map<String, Object>> twoList = relationDao.getRelationBySetMarkATid(paramList.get(i));
						//如果既教正常的又有选课的，就改为3
						if(oneList != null && oneList.size() != 0 && twoList != null && twoList.size() != 0){
							paramList.get(i).put("selectMark", 3);
							relationDao.saveSelectMark(paramList.get(i));
						}
						//0616这个教师与这个课，只是选课类型
						if((oneList == null || oneList.size() == 0) && (twoList != null && twoList.size() != 0)){
							//只教选课的，改为2
							paramList.get(i).put("selectMark", 2);
							relationDao.saveSelectMark(paramList.get(i));
						}
						//0616如果这个教师，这个课没有正常关联，也没有选课课表信息
						if((oneList == null|| oneList.size() == 0) && (twoList == null || twoList.size() == 0) && Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2){
							//修改老师的关系为选课的类型
							paramList.get(i).put("selectMark", 2);
							relationDao.saveSelectMark(paramList.get(i));
						}
						//0616结束
						resultMap.put("result", true);
						//选课班级名字重复
					}else{
						resultMap.put("result", false);
						resultMap.put("failMsg", "班级名称冲突");
						break;
					}
				}
				//20180913添加课与教师的权重
				paramMap.put("classPerformance",0.4);
				paramMap.put("excellent",100);
				paramMap.put("good",90);
				paramMap.put("inso",80);
				paramMap.put("bad",60);
				paramMap.put("souceMark",0);
				paramMap.put("stuorteaId",paramList.get(0).get("stuorteaId"));
				paramMap.put("courseId",paramList.get(0).get("courseId"));
				//查询这个教师这个课程是否已经存在权重
				Map<String,Object> sourceMap = sourceDao.getProWedith(paramMap);
				if(sourceMap==null||sourceMap.size()==0){
					sourceDao.addSorceSet(paramMap);
				}
			}else{
				resultMap.put("result", false);
				resultMap.put("failMsg", addMarkMap.get("failMsg"));
			}
			logger.info("保存手动排课，参数为："+paramList.toString());
		} catch (Exception e) {
			logger.error("保存手动排课异常saveClassTab", e);
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 获取课表集合是否有冲突不可排课的标识
	 * 1时间地点冲突，教师选择错误，3 正常排课的时候没有关联教师与课程的信息
	 * @param Map<String,Object> paramMap
	 * @return （true可以排课  false不可排课）
	 * @throws Exception
	 */
	public Map<String, Object> getSaveTabMark(List<Map<String, Object>> paramList) throws Exception {

		//操作结果标识
		boolean result = true;
		//保存参数长度
		int originLength = paramList.size();
		//循环中用到的值
		int trueLength = 0;
		//循环中用到的集合信息
		List<Map<String, Object>> list;
		//存放错误信息
		String errorMsg = "";
		//存放返回值信息
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			TreeSet<ClassTabVO> treeSet = new TreeSet<ClassTabVO>(new MyComparator());
			ClassTabVO tabVO ;
			for(int i = 0; i < paramList.size(); i++){
				//查询有没有老师没有被关联在classrelation表中，如果没有不让排课
				if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 1){
					//0703修改，要查询的是这个老师与这个课程在classrelation表中是否有记录，没有就算没有关联信息
					List<Map<String, Object>> teachClassList = relationDao.getTeachClassByCidAndSid(paramList.get(i));
					if(teachClassList == null || teachClassList.size() == 0){
						resultMap.put("result", false);
						//将保存好教师id的集合传回去
						resultMap.put("paramList", paramList);
						resultMap.put("failMsg", "教师与课程关联信息未填加");
						return resultMap;
					}
				}
				//2 看选修的是不是安排了多个老师的问题
				if(Integer.parseInt(paramList.get(i).get("selectMark").toString()) == 2){
					//查询这个课这个类型的其他上课老师有多少  还有3
					List<Map<String, Object>> teacherList = relationDao.getCourseTeaOtherList(paramList.get(i));
					if(teacherList != null && teacherList.size() != 0){
						resultMap.put("result", false);
						//将保存好教师id的集合传回去
						resultMap.put("paramList", paramList);
						resultMap.put("failMsg", "教师选择错误");
						return resultMap;
					}
				}
				//3 看参数中选修类型是不是选择了多个教师信息
				tabVO = new ClassTabVO();
				tabVO.setCourseId(Integer.parseInt(paramList.get(i).get("courseId").toString()));
				tabVO.setSelectMark(Integer.parseInt(paramList.get(i).get("selectMark").toString()));
				tabVO.setStuorteaId(paramList.get(i).get("stuorteaId").toString());
				treeSet.add(tabVO);
				//4 看时间地点是否冲突
				list = relationDao.getOriginalClassTab(paramList.get(i));
				if(list == null){
					trueLength += 1;
				}else{
					if(list.size() == 0){
						trueLength += 1;
					}
				}
			}
			if(paramList != null && treeSet.size() != paramList.size()){
				result = false;
				errorMsg = "教师选择错误";
			}
			if(result == true && originLength != trueLength){
				result = false;
				errorMsg = "时间地点冲突";
			}
			//返回结果
			resultMap.put("result", result);
			//将保存好教师id的集合传回去
			resultMap.put("paramList", paramList);
			if(!result){
				resultMap.put("failMsg", errorMsg);
			}
		} catch (Exception e) {
			logger.error("获取课表集合是否有冲突不可排课的标识异常getSaveTabMark", e);
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 保存自动排课
	 * @param Map<String,Object> paramMap
	 * @return Map<String, String> result true成功，false失败failMsg错误信息
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> saveAutoClassTab(Map<String, Object> paramMapWeb) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		DateUtil dateUtil = new DateUtil();
		try {
			//如果有不合理的排课信息,返回失败信息
			Map<String, Object> okMarkMap = getRationalMark((List<Map<String, Object>>)paramMapWeb.get("tabList"));
			if(!(Boolean)okMarkMap.get("result")){
				return okMarkMap;
			}
			//遍历每一条需要自动排课的参数信息
			for(Map<String, Object> paramMap : (List<Map<String, Object>>)paramMapWeb.get("tabList")) {
				//构造参数
				paramMap.put("startDate", paramMap.get("classDateStart"));
				paramMap.put("endDate", paramMap.get("classDateEnd"));
				//获取选择的时间段内的上课日期（满足星期几的条件的）
				Map<String, Object> dateMap = dateUtil.getWeekByDate(paramMap);
				//获取得到的全部日期集合
				List<String> dateList = (List<String>) dateMap.get("dateList");
				if (dateList.size() != 0) {
					//用于存储可以进行排课的信息
					List<Map<String, Object>> okClassList = new ArrayList<Map<String, Object>>();
					Map<String, Object> useMap;
					//获取要排课的位置课节数
					List<Integer> classNumList = (List<Integer>) paramMap.get("classNumList");
					okClassList = getTabParamFairMark(paramMap, dateList, classNumList);
					//如果存在可排课的课程
					if (okClassList.size() != 0) {
						//保存排课信息
						Map<String,Object> paraMap = new HashMap<>();
						paraMap.put("tabList",okClassList);
						Map<String, Object> addResult = this.saveClassTab(paraMap);
						if ((Boolean) addResult.get("result")) {
							resultMap.put("result", true);
						} else {
							resultMap.put("result", false);
							resultMap.put("failMsg", addResult.get("failMsg"));
							return resultMap;
						}
					} else {
						resultMap.put("result", false);
						resultMap.put("failMsg", "排课地点冲突或教师上课时间冲突");
						return resultMap;
					}
				} else {
					resultMap.put("result", false);
					resultMap.put("failMsg", "当前选择条件下并无可排课日期");
					return resultMap;
				}
			}
			logger.info("保存自动排课，参数为："+paramMapWeb.toString());
		} catch (Exception e) {
			logger.error("保存自动排课异常saveAutoClassTab", e);
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 自动排课参数是否合法的判定方法
	 *
	 * */
	public Map<String, Object> getRationalMark(List<Map<String, Object>> list){
		Map<String, Object> map = new HashMap<String, Object>();
		DateUtil dateUtil = new DateUtil();
		try {
			if(list == null || list.size() == 0){
				map.put("result",false);
				map.put("failMsg", "请选择排课信息");
				return map;
			}
			//看参数中选修类型是不是选择了多个教师信息，还有参数是不是没有符合条件
			Map<String, Object> checkMap = getClassTabVOS(list);
			if(!(Boolean)checkMap.get("result")){
				map.put("result",false);
				map.put("failMsg", checkMap.get("failMsg"));
				return map;
			}else{
				TreeSet<ClassTabVO> treeSet = (TreeSet<ClassTabVO>)checkMap.get("treeSet");
				if(list != null && treeSet.size() != list.size()){
					map.put("result",false);
					map.put("failMsg", "教师选择错误");
					return map;
				}
			}
			//进行操作
			int paramSize = list.size();
			int okList = 0;
			for(Map<String, Object> paramMap : list){
				//构造参数
				paramMap.put("startDate", paramMap.get("classDateStart"));
				paramMap.put("endDate", paramMap.get("classDateEnd"));
				//获取选择的时间段内的上课日期（满足星期几的条件的）
				Map<String, Object> dateMap = dateUtil.getWeekByDate(paramMap);
				//获取得到的全部日期集合
				List<String> dateList = (List<String>)dateMap.get("dateList");
				if(dateList == null || dateList.size() == 0){
					map.put("result",false);
					map.put("failMsg", getErrorTabMsg(paramMap));
					return map;
				}
				//获取要排课的位置课节数
				List<Integer> classNumList = (List<Integer>)paramMap.get("classNumList");
				List<Map<String, Object>> okClassList = getTabParamFairMark(paramMap, dateList, classNumList);
				if(okClassList.size() == 0){
					map.put("result",false);
					map.put("failMsg", "排课地点冲突或教师上课时间冲突");
					return map;
				}
				okList += 1;
			}
			if(okList == paramSize){
				map.put("result",true);
			}else{
				map.put("result",false);
				map.put("failMsg", "排课失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * @description 通过排课给的参数，构建一个是否一个课的选课类型有多个教师的不规则参数，并且检查参数中是否有不规则的情况
	 * @param list 排课参数
	 * */
	private Map<String, Object> getClassTabVOS(List<Map<String, Object>> list) {
		TreeSet<ClassTabVO> treeSet = new TreeSet<ClassTabVO>(new MyComparator());
		ClassTabVO tabVO ;
		Map<String, Object> map = new HashMap<String, Object>();
		for(int i = 0; i < list.size(); i++){
			if(list.get(i).get("courseId") == null || list.get(i).get("stuorteaId") == null){
				map.put("result",false);
				map.put("failMsg", "请选择排课信息请完整选择排课信息");
				return map;
			}
			tabVO = new ClassTabVO();
			tabVO.setCourseId(Integer.parseInt(list.get(i).get("courseId").toString()));
			tabVO.setSelectMark(Integer.parseInt(list.get(i).get("selectMark").toString()));
			tabVO.setStuorteaId(list.get(i).get("stuorteaId").toString());
			treeSet.add(tabVO);
		}
		map.put("result", true);
		map.put("treeSet", treeSet);
		return map;
	}

	/**
	 * @description 获取自动排课的参数是否冲突的标志，返回的是合理的位置，如果没有则是冲突
	 * */
	public List<Map<String, Object>> getTabParamFairMark(Map<String, Object> paramMap, List<String> dateList, List<Integer> classNumList) throws Exception {
		Map<String, Object> useMap;
		List<Map<String, Object>> okClassList= new ArrayList<Map<String, Object>>();
		for(int i = 0; i < dateList.size(); i++){
			for(int j = 0; j < classNumList.size(); j++){
				//构建参数
				useMap = new HashMap<String, Object>();
				useMap.put("classNum", classNumList.get(j));
				useMap.put("classDate", dateList.get(i));
				useMap.put("stuorteaId", paramMap.get("stuorteaId"));
				useMap.put("siteId", paramMap.get("siteId"));
				//查询是否有上课信息
				List<Map<String, Object>> originalTabList = relationDao.getOriginalClassTab(useMap);
				if(originalTabList == null || originalTabList.size() == 0){
					useMap.put("selectMark", paramMap.get("selectMark"));
					useMap.put("departmentId", paramMap.get("departmentId"));
					useMap.put("professionId", paramMap.get("professionId"));
					useMap.put("courseId", paramMap.get("courseId"));
					useMap.put("classId", paramMap.get("classId"));
					useMap.put("mainId", paramMap.get("mainId"));
					okClassList.add(useMap);
				}
			}
		}
		return okClassList;
	}

	/**
	 * 获取失败信息
	 * */
	public String getErrorTabMsg(Map<String, Object> map){
		DateUtil dateUtil = new DateUtil();
		Map<String, Object> useMap = new HashMap<String, Object>();
		StringBuffer sb = new StringBuffer("");
		try{
			sb.append(courseDao.getCourseDetail(map).get("courseName")).append("课，");
			sb.append(map.get("classDateStart").toString()).append("-").append(map.get("classDateEnd").toString()).append("日期，");
			sb.append(dateUtil.getWeekByDate(((List<Integer>)map.get("weekList")).get(0))).append("的第"+((List<Integer>)map.get("classNumList")).get(0)+"节排课当前条件并无可排课日期");
			return sb.toString();
		}catch(Exception e){
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * @description 查询某时间范围内的地点的上课课表信息
	 * @param Map<String,Object> paramMap
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getSiteClassTab(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		try {
			List<Map<String, Object>> dateList = (List<Map<String, Object>>)paramMap.get("dateList");
			paramMap.put("startDate", dateList.get(0));
			paramMap.put("endDate", dateList.get(dateList.size()-1));
			resultList = relationDao.getSiteClassTab(paramMap);
		} catch (Exception e) {
			logger.error("查询某时间范围内的地点的上课课表信息异常getSiteClassTab", e);
			throw e;
		}
		return resultList;
	}

	/**
	 * @description 管理员的课程管理（排课表的列表可以搜索）
	 * @param Map<String,Object> paramMap
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getManagerCourseTabList(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int pageNow = Integer.parseInt(paramMap.get("pageNow").toString());
			int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取列表信息
			List<Map<String, Object>> tabList = relationDao.getManagerCourseTabList(paramMap);
			resultMap.put("tabList", tabList);
			resultMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			paramMap.put("startNum", 0);
			List<Map<String, Object>> allList = relationDao.getManagerCourseTabList(paramMap);
			int totalPage = pageUtil.getTotalPage(allList, pageSize);
			resultMap.put("totalPage", totalPage);
		} catch (Exception e) {
			logger.error("管理员的课程管理异常getManagerCourseTabList", e);
			throw e;
		}
		return resultMap;
	}



	/**
	 * @description 删除课表新方法
	 * */
	public Map<String,Object> delCourseTabsNew(List<String> list){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try{
			Map<String, Object> relationDetail;
			List<Map<String, Object>> selectTabList;
			for(String relationId : list){
				resultMap.put("relationId", relationId);
				//查询这条课表全部信息
				relationDetail = relationDao.getOneRelationById(resultMap);
				//如果要删除的是选修的课表
				if(Integer.parseInt(relationDetail.get("selectMark").toString()) == 2){
					//查询这个课的选课类型的所有课表信息
					relationDetail.put("type", 5);
					selectTabList = relationDao.getRelaById(relationDetail);
					//如果有选课课表信息
					if(selectTabList != null && selectTabList.size() != 0){
						//如果这个课的选课课表只剩下当前要删除的这一条了
						if(selectTabList.size() == 1 && Integer.parseInt(selectTabList.get(0).get("relationId").toString()) == Integer.parseInt(relationId)){
							//删除与修改关联信息的方法
							this.delRelationBySelectRelationMsg(relationDetail);
						}else{
							//删除课表信息
							relationDao.delClassTabById(resultMap);
						}
					}
					//要删除的课表是正常的课表
				}else{
					//删除课表信息
					relationDao.delClassTabById(resultMap);
				}
			}
			resultMap.put("result", true);
		}catch(Exception e){
			e.printStackTrace();
			logger.info("删除课表异常");
			resultMap.put("result", false);
		}
		return resultMap;
	}

	/**
	 * @description 删除某条课表的时候做的删除信息与修改关联信息的操作
	 *	@param 这条课表详细信息，这条课表的课程的详细信息，这个班级下的全部学生的信息
	 *  */
	public boolean delRelationBySelectRelationMsg(Map<String, Object> relationDetail){
		boolean result = false;
		try {
			//1、删除选修班级
			List<String> classList = new ArrayList<String>();
			classList.add(relationDetail.get("classId").toString());
			classDao.delClass(classList);
			//2、删除课程与班级的关联
			relationDao.delCourseReClass(relationDetail);
			//3、删除老师与班级的关联
			relationDao.delClassReTeacher(relationDetail);
			//4、删除学生与这个课的这个类型的关系
			relationDao.delStuReCourse(relationDetail);
			//6、删除这条课表信息
			relationDao.delClassTabById(relationDetail);
			//7、修改course表状态，修改这个教师与这个课的关联
			this.saveRelationMark(relationDetail);
			//5、删除学生与班级的关联
			relationDetail.put("couSelectMark", 2);
			relationDao.delRelStuClass(relationDetail);
			result = true;
		}catch(Exception e){
			e.printStackTrace();
			result = false;
		}
		return result;
	}

	/**
	 * @description 删除课表的时候修改course与relstucou的标识
	 * */
	public boolean saveRelationMark(Map<String, Object> relationDetail){
		boolean result = false;
		try {
			Map<String, Object> saveParam = new HashMap<String, Object>();
			//1、修改course
			Map<String, Object> courseDetail = courseDao.getCourseDetail(relationDetail);
			if(Integer.parseInt(courseDetail.get("selectMark").toString()) != 1){
				//修改课程为1正常类型
				saveParam.put("courseId" ,courseDetail.get("courseId"));
				saveParam.put("selectMark" ,1);
				courseDao.saveSelectMark(saveParam);
			}
			//2、修改relstucou教师与这个课的关联
			//查询这个教师与这个课程原来的关联信息
			List<Map<String, Object>> relaMapList = relationDao.getRelCourseTea(relationDetail);
			if(relaMapList != null || relaMapList.size() != 0){
				if(Integer.parseInt(relaMapList.get(0).get("couSelectMark").toString()) == 3){
					//修改为1
					saveParam.clear();
					saveParam.put("courseId" ,courseDetail.get("courseId"));
					saveParam.put("stuorteaId" ,relationDetail.get("stuorteaId"));
					saveParam.put("selectMark" ,1);
					relationDao.saveSelectMark(saveParam);
				}else if(Integer.parseInt(relaMapList.get(0).get("couSelectMark").toString()) == 2){
					//如果是录入人
					if(courseDetail.get("stuorteaId").toString().equals(relationDetail.get("stuorteaId").toString())){
						saveParam.clear();
						saveParam.put("courseId" ,courseDetail.get("courseId"));
						saveParam.put("stuorteaId" ,relationDetail.get("stuorteaId"));
						saveParam.put("selectMark" ,1);
						relationDao.saveSelectMark(saveParam);
						//录入人的录入标识改为1 inmark
						relationDetail.put("inMark", 1);
						relationDao.saveInMarkReMark(relationDetail);
						//不是录入人，删除relstucou关联
					}else{
						relationDao.delCourseReTeacher(relationDetail);
					}
				}
			}
			result = true;
		}catch(Exception e){
			e.printStackTrace();
			result = false;
		}
		return result;
	}


	/**
	 * @description 查询全部可选课课程
	 * @param Map<String,Object> paramMap
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> getSelectCourses(Map<String, Object> paramMap) throws Exception{

		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = relationDao.getSelectCourses(paramMap);
		}catch(Exception e){
			logger.error("查询全部可选课课程异常getSelectCourses", e);
			throw e;
		}
		return list;
	}

	/**
	 * @description  查询全部已经排课了的教师的信息
	 * @param Map<String,Object> paramMap
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> getSelectTeacherList(Map<String, Object> paramMap) throws Exception{

		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = relationDao.getSelectTeacherList(paramMap);
		}catch(Exception e){
			logger.error("查询全部已经排课了的教师的信息异常getSelectTeacherList", e);
			throw e;
		}
		return list;
	}

	/**
	 * @description  选课课程的列表
	 * @param Map<String,Object> paramMap
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getSelectList(Map<String, Object> paramMap, HttpServletRequest request) throws Exception{

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int pageNow = Integer.parseInt(paramMap.get("pageNow").toString());
			int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取列表信息
			paramMap.put("stuId", new SessionValues().getSessionValues(new String[] {"stuorteaId"}, request, "userMsg").get("stuorteaId").toString());
			List<Map<String, Object>> selectList = relationDao.getSelectList(paramMap);
			resultMap.put("selectList", selectList);
			resultMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			paramMap.put("startNum", 0);
			List<Map<String, Object>> allList = relationDao.getSelectList(paramMap);
			int totalPage = pageUtil.getTotalPage(allList, pageSize);
			resultMap.put("totalPage", totalPage);
		} catch (Exception e) {
			logger.error("选课课程的列表异常getSelectList", e);
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description  保存学生选课信息
	 * @param Map<String,Object> paramMap
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> saveStuSelectMsg(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//查询该学生之前的所有课表信息与当前选择的课程的这个类型的课表是否有时间相同地点不同的数据
			List<Map<String, Object>> repeatList = relationDao.getRepeatclassTab(paramMap);
			//如果没有继续执行
			if(repeatList == null || repeatList.size() == 0){
				//查询这个课程选了多少人，一共选择多少人
				String stuorteaId = (String)paramMap.get("stuorteaId");
				paramMap.remove("stuorteaId");
				Map<String, Object> courseDetail = courseDao.getCourseDetail(paramMap);
				if(courseDetail != null ){
					int maxStu = Integer.parseInt(courseDetail.get("maxStu").toString());
					long selectNum = 0;
					selectNum = courseDao.getSelectNum(paramMap);
					if(maxStu > selectNum){
						paramMap.put("inMark", 0);
						paramMap.put("stuorteaId", stuorteaId);
						paramMap.put("selectMark", 2);
						//判断之前有没有这个学生这个课的关联，如果没有，添加，如果有，修改标识为2
						List<Map<String, Object>> relStuCouList = relationDao.getRelCourseTea(paramMap);
						if(relStuCouList == null || relStuCouList.size() == 0){
							courseDao.addRelTeaCourse(paramMap);
						}else{
							//如果有学生课程的关联，需要将关系标识修改为2
							relationDao.saveSelectMark(paramMap);
							//将是否结课的标识改为0
							paramMap.put("endMark", 0);
							classDao.saveEndMark(paramMap);
						}
						//0510将学生与选课班级的关联加进去
						//查询这个学生与这个班级之前有没有过关联信息
						List<Map<String, Object>> selectClassMark = relationDao.getStuSelectClassList(paramMap);
						if(selectClassMark == null || selectClassMark.size() == 0){
							courseDao.addSelectRestuClass(paramMap);
						}
						resultMap.put("result", true);
					}else{
						resultMap.put("result", false);
						resultMap.put("failMsg", "课程人数已满");
					}
				}
			}else{
				resultMap.put("result", false);
				resultMap.put("failMsg", "当前选择课程与课表冲突，无法继续选择");
			}
			logger.info("保存学生选课信息，参数为："+paramMap.toString());
		} catch (Exception e) {
			logger.error("保存学生选课信息异常saveStuSelectMsg", e);
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description  查询某条课程表的全部信息只要id就可以
	 * @param Map<String,Object> paramMap   relationId
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getOneRelationById(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = relationDao.getOneRelationById(paramMap);
		} catch (Exception e) {
			logger.error("查询某条课程表的全部信息异常getOneRelationById", e);
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description  修改一节课程表信息的
	 * @param Map<String,Object> paramMap   relationId 等全部课表信息
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> saveOneCourseTab(Map<String, Object> paramMap)throws Exception{

		Map<String, Object> map = new HashMap<String, Object>();
		try{
			//获取上课时间的参数
			Map<String, Object> paramTimeMap = paramActionImpl.matchClassNumAndTime();
			List<Map<String, Object>> paramList = new ArrayList<Map<String,Object>>();
			paramList.add(paramMap);
			//查询是否可以进行排课（地点是否冲突，教师是否有课）
			boolean saveMark = (Boolean)this.getSaveTabMark(paramList).get("result");
			//可以进行排课
			if(saveMark){
				paramMap.put("startTime", paramTimeMap.get(paramMap.get("classNum").toString()+"start"));
				paramMap.put("endTime", paramTimeMap.get(paramMap.get("classNum").toString()+"end"));
				//修改课表
				relationDao.saveClassTabOne(paramMap);
				map.put("result", true);
			}else{
				map.put("result", false);
				map.put("failMsg", "排课地点冲突或教师上课时间冲突");
			}
			logger.info("修改一节课程表信息,参数为："+paramMap.toString());
		}catch(Exception e){
			logger.error("修改一节课程表信息异常saveOneCourseTab", e);
			throw e;
		}
		return map;
	}



	/**
	 * @description  给出该学生是否选择了课程标识
	 * @param Map<String,Object> paramMap   relationId
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> setSelectMark(List<Map<String, Object>> paramList,HttpServletRequest request) throws Exception{

		try {
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request, "userMsg");
			for(int i=0;i<paramList.size();i++){
				sessionValues.put("courseId", paramList.get(i).get("courseId"));
				List<Map<String, Object>> relationList = relationDao.getRelStuCourseListById(sessionValues);
				if(relationList != null && relationList.size() != 0){
					//0626查询这个血红色那个所在的全部班级与这个课的未结课的信息有没有，如果有就是选择过，不能选了，true
					List<Map<String, Object>> selectNotEndList = relationDao.getRelationStuSelectMark(sessionValues);
					if(selectNotEndList != null && selectNotEndList.size() != 0){
						paramList.get(i).put("selectMark", true);
						//0713查询这个学生与这个课的关系，selectMark
						List<Map<String, Object>> stuSelectMarkList = relationDao.getRelCourseTea(sessionValues);
						if(stuSelectMarkList != null && stuSelectMarkList.size() != 0){
							if(Integer.parseInt(stuSelectMarkList.get(0).get("couSelectMark").toString()) == 1){
								paramList.get(i).put("selectMark", true);
								paramList.get(i).put("selectState", 1);
							}else{
								paramList.get(i).put("selectMark", true);
								paramList.get(i).put("selectState", 2);
							}
						}
						//0713end
					}else{
						paramList.get(i).put("selectMark", false);
					}
					//0626end
				}else{
					paramList.get(i).put("selectMark", false);
				}
			}
		} catch (Exception e) {
			logger.error("给出该学生是否选择了课程标识方法异常setSelectMark", e);
			throw e;
		}
		return paramList;
	}



	/**
	 * @description  修改课程的标识
	 * @param Map<String,Object>
	 * @return
	 * @throws Exception
	 */
	public boolean saveCourseSelect(Map<String, Object> paramMap) throws Exception{

		boolean result = false;
		try{
			//paramMap.put("selectType", 1);
			//查询该课程的正常关联
			List<Map<String, Object>> normalList = relationDao.relationList(paramMap);
			paramMap.put("selectType", 2);
			//查询该课程的选课课表
			List<Map<String, Object>> selectList = relationDao.selectTabList(paramMap);
			//如果是选课和正常上课都有 ，就将标识存为3
			if(normalList != null && normalList.size() != 0 && selectList != null && selectList.size() != 0){
				paramMap.put("selectMark", 3);
				courseDao.saveSelectMark(paramMap);
			}
			//如果只有选课的，就改为2
			if((normalList == null || normalList.size() == 0) && selectList != null && selectList.size() != 0){
				paramMap.put("selectMark", 2);
				courseDao.saveSelectMark(paramMap);
			}
			result = true;
		}catch(Exception e){
			logger.error("修改课程的标识异常saveCourseSelect", e);
			throw e;
		}
		return result;
	}



	/**
	 * @description  查询某个课表某节里面对应的选课班级，是查询出来不是就无结果
	 * @param Map<String,Object>
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getSelectClassOne(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try{
			resultMap = relationDao.getSelectClassOne(paramMap);
		}catch(Exception e){
			logger.error("查询某个课表某节里面对应的选课班级异常getSelectClassOne",e);
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description  生成选课班级名称
	 * 规则：用课程名称+当前2位日期+申请班  如果有这个名字的班级，就用课程名+当前时间戳
	 * @param Map<String,Object>
	 * @return
	 * @throws Exception
	 */
	public String getRandomClassName(Map<String, Object> paramMap) throws Exception{
		String result = null;
		try{
			Date dt=new Date();
			SimpleDateFormat matter1=new SimpleDateFormat("dd");
			String day = matter1.format(dt);
			String timestamp = String.valueOf(System.currentTimeMillis());
			//通过课程id获取课程名字信息
			Map<String, Object> courseDetail = courseDao.getCourseDetail(paramMap);
			String courseName = (String)courseDetail.get("courseName");
			String oneSelect = courseName + day + "申请班";
			List<Map<String, Object>> classList = classDao.getClassByClassName(oneSelect);
			//如果存在这个名字的班级，就更换下一个
			if(classList != null && classList.size() != 0){
				result = courseName + timestamp;
			}else{
				result = oneSelect;
			}
		}catch(Exception e){
			logger.error("生成选课班级名称异常getRandomClassName",e);
			throw e;
		}
		return result;
	}

	/**
	 * @description  查询全部教师与课程之间的关系列表
	 * @param Map<String,Object>
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getTeaReCourseList(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = Integer.parseInt(paramMap.get("pageNow").toString());
			int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取老师与课程的列表信息
			List<Map<String, Object>> reList = relationDao.getTeaReCourseList(paramMap);
			returnMap.put("reList", reList);
			returnMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allReList = relationDao.getTeaReCourseList(paramMap);
			int totalPage = pageUtil.getTotalPage(allReList, pageSize);
			returnMap.put("totalPage", totalPage);
		} catch (Exception e) {
			logger.error("查询全部教师与课程之间的关系列表异常getTeaReCourseList",e);
			throw e;
		}
		return returnMap;
	}



	/**
	 * @description 课程库
	 * @param Map<String,Object>
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> courseList(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = Integer.parseInt(paramMap.get("pageNow").toString());
			int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			paramMap.put("releaseMark", 1);
			//获取老师与课程的列表信息
			List<Map<String, Object>> courseList = relationDao.courseList(paramMap);
			returnMap.put("courseList", courseList);
			returnMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allReList = relationDao.courseList(paramMap);
			int totalPage = pageUtil.getTotalPage(allReList, pageSize);
			returnMap.put("totalPage", totalPage);
		} catch (Exception e) {
			logger.error("课程库查询异常courseList", e);
			throw e;
		}
		return returnMap;
	}


	/**
	 * @description 删除作业文件方法
	 * */
	public void delHomework(Map<String, Object> paramMap){
		try {
			//3.0删除作业文件信息查询这个班级下所有学生这个课的作业信息
			List<Map<String, Object>> workList = courseDao.getStuWorkByCourseIdAndClassId(paramMap);
			DelWorkFileUtil delWorkFileUtil = new DelWorkFileUtil();
			//文件前缀路径
			String[] keysArray = {"server_address", "project_name", "tomcatUrl"};
			Map<String, Object> urlmap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
			String url = (String) urlmap.get("tomcatUrl") + (String) urlmap.get("project_name") + "upload/stuwork/";
			//删除这些学生的作业文件
			delWorkFileUtil.delWorkFile(workList, "taskAddress", "convertFile", url);
			//删除作业文件信息完毕
		}catch(Exception e){
			e.printStackTrace();
			logger.error("解绑时删除学生作业信息异常", e);
		}
	}

	/**
	 * @description 解绑新方法
	 *
	 * */
	public Map<String, Object> unbindFunctionNew(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> returnMap = new HashMap<>();
		try{
			List<String> listOne = new ArrayList<>();
			listOne.add(String.valueOf(paramMap.get("classId")));
			List<Map<String, Object>> classListOne = classDao.getClassStateList(listOne);
			if(classListOne!=null&&classListOne.size()!=0){
				returnMap.put("result",false);
				returnMap.put("failMsg","班级处于考试状态不可解除任课");
				return returnMap;
			}
			//要解绑的授课类型
			int couSelectMark = Integer.parseInt(paramMap.get("couSelectMark").toString());
			//删除学生上传的作业文件信息
			this.delHomework(paramMap);
			//删除这个班级下的所有学生的这个课的作业信息
			homeworkTestDao.delTaskByCourseIdAndClassId(paramMap);
			//查询这个班级下的学生信息
			List<String> list = homeworkTestDao.getClassStu(paramMap);
			if(!CollectionUtils.isEmpty(list)){
				Map<String, Object> delMap = new HashMap<String, Object>();
				delMap.put("delType", 2);
				delMap.put("list", list);
				homeworkTestDao.delStuTestHistory(delMap);
			}

			//删除试卷一些列
			List<Map<String,Object>> onlineList = relStuClassDao.getAllClassStateByTeaCouId(paramMap);
			//说明当前教师彻底不教这个课这个班了，则将这个课这个试卷的关联、这个课这个班的成绩等删除
			//通过试卷主键查询stuanswerquestion表中的学生id
//			List<Map<String,Object>> stuIdList = questionDao.getStuAnswerByOnIdListObject(onlineList);
			paramMap.put("list",onlineList);
			//查询这个课这个班的学生id
			List<Map<String,Object>> stuIdList = relStuClassDao.getSIdListByClassIdAndCoId(paramMap);
			List<Map<String,Object>> questionist = (List<Map<String, Object>>) paramMap.get("list");
			if(stuIdList!=null&&stuIdList.size()!=0){
				//删除这个教师教的这个课的所有学生的成绩信息
				sourceDao.delStuSoruceBystuorteaId(stuIdList);
			}
			//删除这个教师教的这个课这个班的班级与试卷的关联信息
			if(questionist!=null&&questionist.size()!=0){
				//删除这个教师教的这个课的这个班级下的所有考试学生的答题信息
				paramMap.put("onlineMark",1);
				paramMap.put("stuIdList",stuIdList);
				questionDao.delStuanswerByOnlineIdTwo(paramMap);
				questionDao.delreClassOnlineByCouId(paramMap);
			}
			//说明这个教师这个课程的这个试卷已经不存在和班级有关联关系（相当于这个教师彻底不教这个课了），则将这个课的这个教师的这个试卷删除
//			questionDao.delOnlineByTeaId(paramMap);




			//如果当前类型是1 教师排课
			if(couSelectMark == 1){
				//删除classrelation表中的关联信息
				classDao.delClassRelation(paramMap);
				//删除这个班级下的学生、这个课、这个类型的关联信息relstucou
				classDao.delStuReCouByClassId(paramMap);
			}
			//如果是类型2 选课类型，删除这个选课班级下的学生与这个课的关联
			if(couSelectMark == 2){
				//删除这个班级下的学生、与这个课、这个类型的关联
				classDao.delStuReCouByClassId(paramMap);
				//删除选课班级
				paramMap.put("myDelMark", 4);
				relationDao.delSelectRel(paramMap);
			}
			//删除这个课这个班级的关联
			relationDao.delCourseReClass(paramMap);
			//删除这个老师与这个班级的关联信息，这个课的关联信息
			relationDao.delClassReTeacher(paramMap);
			//删除这个班级下的学生的这个课的成绩
			homeworkTestDao.delExamByClassIdAndCourseId(paramMap);
			//删除这个班级下的学生的学习历史信息，
			homeworkTestDao.delClassStudyHistoryByCourseId(paramMap);
			//删除这个班级与学生的关系relstuclass(条件是选修的班级,正常的班级的话不删除)
			classDao.delRelstuclassByClassId(paramMap);
			//如果老师这个课不教其他班级了，就删除学习历史信息
			List<Map<String, Object>> classesList = relationDao.getTeachClassByCourseId(paramMap);
			if(classesList == null || classesList.size() == 0){
				relationDao.delstudyHistoryByCourseIdAndTeacherId(paramMap);
			}
			Map<String, Object> courseDetail = courseDao.getCourseDetail(paramMap);
			//修改relstucou(只修改不删除)
			this.saveRelStuCou(paramMap);
			//修改course类型
			this.saveCourseSelectMark(paramMap);
			//如果这个老师不教别的班级这个课，这个类型的了，就删除这个老师的课表
			List<Map<String, Object>> classList = relationDao.getTeacherClasses(paramMap);
			if(classList == null || classList.size() == 0){
				paramMap.put("delType", 1);
				relationDao.delRelation(paramMap);
			}
			//查询这个教师与这个课是否还有关联
			List<Map<String, Object>> teachList = relationDao.getCourseReTeacher(paramMap);
			if(teachList == null || teachList.size() == 0){
				//删除这个课与这个老师的学习历史信息
				relationDao.delstudyHistoryByCourseIdAndTeacherId(paramMap);
				//如果不是录入人，删除这个老师与这个课程的关联
				if(!paramMap.get("stuorteaId").toString().equals(courseDetail.get("stuorteaId").toString())){
					relationDao.delCourseReTeacher(paramMap);
				}else{
					//如果是录入人，将inMark改为1
					paramMap.put("inMark", 1);
					relationDao.saveInMarkReMark(paramMap);
				}
			}
			returnMap.put("result",true);
			returnMap.put("failMsg","");
			logger.info("解绑方法，参数为："+paramMap.toString());
		}catch(Exception e){
			logger.error("解绑教师与这个班级这个课程关联的方法异常unbindFunction", e);
			throw e;
		}
		return returnMap;
	}

	/**
	 * @description 用于解绑，修改relstucou 教师与课程之间的关系，
	 * */
	public void saveRelStuCou(Map<String, Object> map){

		try{
			Map<String, Object> mapParam = new HashMap<String, Object>();
			mapParam.putAll(map);
			mapParam.put("couSelectMark", 1);
			//查询这个老师与这个课正常排课类型的信息
			List<Map<String, Object>> teachList = relationDao.getTeaRelationByType(mapParam);
			mapParam.put("couSelectMark", 2);
			//查询这个老师与这个课选课类型的信息
			List<Map<String, Object>> selectList = relationDao.getTeaRelationByType(mapParam);
			if(teachList == null || teachList.size() == 0){
				if(selectList == null || selectList.size() == 0){
					mapParam.put("selectMark", 1);
					relationDao.saveSelectMark(mapParam);
				}else{
					mapParam.put("selectMark", 2);
					relationDao.saveSelectMark(mapParam);
				}
			}else{
				if(selectList == null || selectList.size() == 0){
					mapParam.put("selectMark", 1);
					relationDao.saveSelectMark(mapParam);
				}else{
					mapParam.put("selectMark", 3);
					relationDao.saveSelectMark(mapParam);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error("解绑时修改教师与课程的关联异常saveRelStuCou", e);
		}
	}

	/**
	 * @description 用于解绑，修改course的selectMark
	 * */
	public void saveCourseSelectMark(Map<String, Object> map){

		try{
			Map<String, Object> mapParam = new HashMap<String, Object>();
			mapParam.putAll(map);
			mapParam.put("couSelectMark", 1);
			//查询这个课的教师排课类型的关联
			List<Map<String, Object>> teachUseList = relationDao.getCourseRelationByType(mapParam);
			//查询这个课的选课类型的关联
			mapParam.put("couSelectMark", 2);
			List<Map<String, Object>> selectUseList = relationDao.getCourseRelationByType(mapParam);
			if(teachUseList == null || teachUseList.size() == 0){
				if(selectUseList == null || selectUseList.size() == 0){
					mapParam.put("selectMark", 1);
					courseDao.saveSelectMark(mapParam);
				}else{
					mapParam.put("selectMark", 2);
					courseDao.saveSelectMark(mapParam);
				}
			}else{
				if(selectUseList == null || selectUseList.size() == 0){
					mapParam.put("selectMark", 1);
					courseDao.saveSelectMark(mapParam);
				}else{
					mapParam.put("selectMark", 3);
					courseDao.saveSelectMark(mapParam);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error("解绑时修改课程标志异常saveCourseSelectMark", e);
		}
	}





	/**
	 * @description 获取这个学生，这个班级，这个课，是哪个老师教，
	 * @param Map<String,Object>  课id，类型,班级id，老师id
	 * @return boolean
	 * @throws Exception
	 */
	public Map<String, Object> getMyTeacherByCourseId(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> map = new HashMap<String, Object>();
		try{
			//如果是学生
			if(Integer.parseInt(paramMap.get("roleId").toString()) == 1){
				//查询他所在班级的这个课程的教课老师id
				List<Map<String, Object>> list = relationDao.getMyTeacherByCourseId(paramMap);
				if(list != null && list.size() != 0){
					map.put("stuorteaId", list.get(0).get("stuorteaId"));
				}
			}
		}catch(Exception e){
			logger.error("获取这个学生这个班级这个课是哪个老师教异常getMyTeacherByCourseId", e);
			throw e;
		}
		return map;
	}

	/**
	 * @description 学生退选课程
	 * @param request
	 * @param response
	 * @param jsonStr  jsonStr={"courseId":课程id}
	 * */
	public void stuReturnCourse (HttpServletRequest req, HttpServletResponse res, String jsonStr){
		//获取参数
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//日志信息
		org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(RelationActionImpl.class.getName());
		try {
			paramMap = mapper.readValue(jsonStr, paramMap.getClass());
			paramMap.put("stuorteaId",new SessionValues().getSessionValues(new String[] {"stuorteaId"}, req, "userMsg").get("stuorteaId"));
			//查询这个学生与这个课程的关联标志是什么类型
			List<Map<String, Object>> relationList = relationDao.getRelCourseTea(paramMap);
			if(relationList != null && relationList.size() != 0){
				if(Integer.parseInt(relationList.get(0).get("couSelectMark").toString()) == 1){
					courseActionImpl.responseStream(res, "{\"result\":\"error\",\"failMsg\":\"班级正常课程不可退选\"}");
					return;
				}
			}
			if(!getStudyMark(paramMap, new String[] {"userstudyhistory","task"})){
				//学生退选选课信息  删除relstucou  relstuclass
				delStuSelectCourse(paramMap);
				courseActionImpl.responseStream(res, "{\"result\":\"success\"}");
			}else{
				courseActionImpl.responseStream(res, "{\"result\":\"error\",\"failMsg\":\"已经进行课程不可退选\"}");
			}
			logger.info("退选课程，参数为："+jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("退选课程异常stuReturnCourse", e);
			courseActionImpl.responseStream(res, "{\"result\":\"error\",\"failMsg\":\"系统异常\"}");
		}
	}

	/**
	 * @description 查询这个学生，这个课程是不是有学习过的迹象，包括学习记录，包括作业信息，包括实验信息
	 * @param stuorteaId    courseId
	 * @param 要查询的位置表名  类型为选课的类型的
	 * @return true 学习过  false没学习过
	 * */
	public boolean getStudyMark(Map<String, Object> paramMap, String[] selectPosition) throws Exception{
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		for(String tabName : selectPosition){
			paramMap.put("tabName",tabName);
			resultList.addAll(relationDao.getDataByTabName(paramMap));
		}
		if(resultList != null && resultList.size() != 0){
			return true;
		}
		return false;
	}

	/**
	 * @description 学生退选选课信息
	 * @param stuorteaId    courseId
	 * @return
	 * */
	public boolean delStuSelectCourse(Map<String, Object> paramMap) throws Exception{
		//删除学生与这个课程的关联，标志是2
		paramMap.put("couSelectMark", 2);
		relationDao.delRelStuCou(paramMap);
		//删除学生与这个选课班级的关联 （sql查询出这个课的选课班级信息）
		relationDao.delStuReSelectClassByCId(paramMap);
		return true;
	}

	/**
	 * @description 删除班级的时候将这个放在最前面调用
	 * @param list List<String> 班级id集合
	 * @param relaList 要删除的所有班级下的班级id 教师Id 课程id集合
	 * */
	public boolean saveTeaReCourseMark(List<String> list, List<Map<String, Object>> relaList){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//List<Map<String, Object>> relaList = new ArrayList<Map<String, Object>>();
			for (String classId : list) {
				map.put("classId", classId);
				//得到这个班级下对应的课程教师班级信息
				//relaList = classDao.getClassRelation(map);
				//遍历这个班级下的全部教师课程关联
				saveCourseReTeaMark(relaList);
			}
			//修改course类型
			saveCourseMark(relaList);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * @description 获取要删除的这些班级下的所有任课关联信息,
	 * */
	public List<Map<String, Object>> getDelRelation(List<String> list){
		List<Map<String, Object>> listReturn = new ArrayList<Map<String, Object>>();
		try{
			listReturn = classDao.getClassRelationByList(list);
		}catch(Exception e){
			e.printStackTrace();
		}
		return listReturn;
	}

	/**
	 * @description 修改这些教师与课程的关联信息
	 * */
	public boolean saveCourseReTeaMark(List<Map<String, Object>> relaList){
		List<Map<String, Object>> otherList = new ArrayList<Map<String, Object>>();
		try {
			for (Map<String, Object> relationMap : relaList) {
				//查询这个课这个教师是否在别的班级里有正常的任课信息
				otherList = classDao.getTeaReClassNormal(relationMap);
				//如果没有这个课这个教师在别的班级里的正常任课信息，看这个教师与这个课的关联是不是3，如果是3改为2，
				if (otherList == null || otherList.size() == 0) {
					//查询教师与这个课程的关联信息，
					otherList = relationDao.getRelCourseTea(relationMap);
					//如果这个教师与这个课的关联是3，修改为2
					if (otherList != null && otherList.size() != 0) {
						if (Integer.parseInt(otherList.get(0).get("couSelectMark").toString()) == 3) {
							relationMap.put("selectMark", 2);
							relationDao.saveSelectMark(relationMap);
						}
					}
				}
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * @description 修改course类型
	 * */
	public boolean saveCourseMark(List<Map<String, Object>> relaList){
		List<Map<String, Object>> otherList = new ArrayList<Map<String, Object>>();
		Map<String, Object> courseMap = new HashMap<String, Object>();
		try {
			for(Map<String, Object> courseIdMap : relaList){
				//查询当前这个course在classRelation是否有与班级的关联，如果有，不改，如果没有，查询这个课原来是不是3，如果是3修改为2
				otherList = classDao.getClassRelationByCId(courseIdMap);
				//如果没有了这个课余其他班级的关联
				if(otherList == null || otherList.size() == 0){
					//查询课程信息，
					courseMap = courseDao.getCourseDetail(courseIdMap);
					//如果原来是3的类型，修改为2
					if(Integer.parseInt(courseMap.get("selectMark").toString()) == 3){
						courseMap.put("selectMark", 2);
						courseDao.saveSelectMark(courseMap);
					}
				}
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * @description 获取删除课程的集合中有没有正在授课的课程信息
	 * */
	public Map<String, Object> getDelCourseMark (List<String> list){
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String failMsg = "";
		try{
			List<Map<String, Object>> relationList = relationDao.getDelCourseMark(list);
			if(relationList == null || relationList.size() == 0){
				returnMap.put("result" , "success");
				returnMap.put("failMsg" , "");
				return returnMap;
			}else{
				for(int i=0;i<relationList.size();i++){
					if(i==relationList.size()-1){
						failMsg +=relationList.get(i).get("courseName");
					}else {
						failMsg +=relationList.get(i).get("courseName")+",";
					}
				}
				returnMap.put("result" , "fail");
				returnMap.put("failMsg",failMsg+"有授课信息是否要删除?");
				return returnMap;
			}
		}catch(Exception e){
			e.printStackTrace();
			return returnMap;
		}
	}

}